package com.safeluck.scannerqcr.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.adapter.ChatAdapter;
import com.safeluck.scannerqcr.bean.Chat;
import com.safeluck.scannerqcr.fragment.msg.MessageFragment;
import com.safeluck.scannerqcr.fragment.shop.MenuFragment;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;


/**
 * ScannerQCR
 *
 * @author lzw
 * @date 2017/11/28
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class WechatFirstTabFragment extends BaseMainFragment implements SwipeRefreshLayout.OnRefreshListener {


    private Toolbar mToobar;
    private SwipeRefreshLayout mSwipeRefrshLayout;
    private RecyclerView mRecyclerView;
    private ChatAdapter chatAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wechat_fragment_first_tab,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mToobar = view.findViewById(R.id.toolbar);
        mSwipeRefrshLayout = view.findViewById(R.id.refrsh_layout);
        mRecyclerView = view.findViewById(R.id.recy);

        mToobar.setTitle("Home");
    }

    public static WechatFirstTabFragment newInstance(){
        Bundle bundle = new Bundle();
        WechatFirstTabFragment wechatFirstTabFragment = new WechatFirstTabFragment();
        wechatFirstTabFragment.setArguments(bundle);
        return wechatFirstTabFragment;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mSwipeRefrshLayout.setOnRefreshListener(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));

        //不是瀑布流时 都要设置true
        mRecyclerView.setHasFixedSize(true);

        final int space = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,0.5f,getResources().getDisplayMetrics());

        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,space);
            }
        });
        chatAdapter = new ChatAdapter(_mActivity);
        chatAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view, RecyclerView.ViewHolder viewHolder) {
                ((MainFragment)getParentFragment()).startBrotherFragment(MessageFragment.newInstance(chatAdapter.getItem(position)));
            }
        });
        mRecyclerView.setAdapter(chatAdapter);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        chatAdapter.setDatas(initDatas());
    }

    private List<Chat> initDatas() {
        List<Chat> msgList = new ArrayList<>();

        String[] name = new String[]{"Jake", "Eric", "Kenny", "Helen", "Carr"};
        String[] chats = new String[]{"message1", "message2", "message3", "message4", "message5"};

        for (int i = 0; i < 15; i++) {
            int index = (int) (Math.random() * 5);
            Chat chat = new Chat();
            chat.name = name[index];
            chat.message = chats[index];
            msgList.add(chat);
        }
        return msgList;
    }

    @Override
    public void onRefresh() {
        mSwipeRefrshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
              mSwipeRefrshLayout.setRefreshing(false);
            }
        },1000);
    }
}
